<?php


function ls_load($filename=false){
	if(!filename && file_exists($filename)){
		if(!in_array($filename,$GLOBALS['lsload_history'])){
			$GLOBALS['lsload_history'][] = $filename;
			include_once($filename);
			return true;
		}
	}
	return false;
}

function ls_import($import=false){
	return ls_load(LIBRARY_DIR.'/'.$import.'.php');
}


function sugm_js_warning_redirect($message='',$url=''){
	print "<script type=\"javascript\">alert('".addslashes($message)."');location.href='$url';</script>";
}
function sugm_js_redirect($url=''){
	print "<script type=\"javascript\">location.href='$url';</script>";
}
function sugm_js_goback(){
	global $Config;
	$u = $Config['site_path'];
	if(strlen($_SERVER['HTTP_REFERER'])>0){
		$u = $_SERVER['HTTP_REFERER'];
	}elseif(strlen($GLOBALS['forward'])>0){
		$u = rawurldecode($GLOBALS['forward']);
	}elseif(strlen($_POST['forward'])>0){
		$u = rawurldecode($_POST['forward']);
	}elseif(strlen($_GET['forward'])>0){
		$u = rawurldecode($_GET['forward']);
	}
	sugm_js_redirect($u);
}
function sugm_get_current_page(){
	$p = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	return $p;
}
function sugm_require_login(){
	global $Config;
	global $sugm_userprofile, $sugm_userrole, $sugm_logstatus;
	if(!$sugm_logstatus){
		$forward = rawurlencode(sugm_get_current_page());
		sugm_js_redirect($Config['login_path']."?forward=$forward");
		exit();
	}
}
define('SUGM_ROLE_REQUIRE_ALL',1011);
define('SUGM_ROLE_REQUIRE_ANY',1012);
define('SUGM_ROLE_REQUIRE_NOT',1013);
function sugm_require_role($data = false, $option=SUGM_ROLE_REQUIRE_ANY){
	if(!$data ){return true;}
	if(is_array($data)){
		
	}else{
		
	}
	return true;
}

define('SUGM_LOGIN_UNKOWN_ERROR',1000);
define('SUGM_LOGIN_NAME_NOTFOUND',1001);
define('SUGM_LOGIN_PASSWORD_INCORRECT',1002);
define('SUGM_LOGIN_NOTACTIVED',1003);
define('SUGM_LOGIN_BANNED',1004);

function sugm_user_auth($username='',$password=''){
	global $context;
	
	$sql = "Select user_id, user_pass, user_name "
	." From ".$Config['db_table_user']." "
	." Where user_name = '".addslashes($username)."' ";
	$userRs = $context->storage->query($sql);
	if($userRs->Count>1){
		return SUGM_LOGIN_UNKOWN_ERROR;
	}elseif($userRs->Count<1){
		return SUGM_LOGIN_NAME_NOTFOUND;
	}
	$userRow = $userRs->fetchRow();
	if($userRow['user_pass']!=md5($password)){
		return SUGM_LOGIN_PASSWORD_INCORRECT;
	}

	return true;
}
function sugm_current_load(){
	global $context;
	global $sugm_userprofile, $sugm_userrole, $sugm_logstatus;
	if(!$context->sessions->get('sugm_uid')){
		$sugm_logstatus = false;
		$sugm_userprofile = array();
		$sugm_userrole = array();
		return false;
	}
	$sugm_logstatus = true;
	$sugm_userprofile = array();

	$sql = "Select user_name,up.* "
	." From {$Config['db_table_user']} u, {$Config['db_table_userprofile']} up"
	." Where u.user_id = '".$context->sessions->get('sugm_uid')."' "
	." And u.user_id = up.user_id ";
	$userRs = $context->storage->query($sql);
	if($userRs->Count!=1){
		return false;
	}
	$sugm_userprofile = $userRs->fetchRow();

	return true;
}
function sugm_user_islogged(){
	global $sugm_logstatus;
	return ($sugm_logstatus);
}
function sugm_user_login($username=''){
	global $context;

	$sql = "Select user_id, user_pass, user_name "
	." From {$Config['db_table_user']} "
	." Where user_name = '".addslashes($username)."' ";
	$userRs = $context->storage->query($sql);
	if($userRs->Count!=1){
		return false;
	}
	$userRow = $userRs->fetchRow();
	$context->sessions->update('sugm_uid',$userRow['user_id']);
	$context->sessions->update('sugm_lastlog',time());
	sugm_current_load();
	return true;
}
function sugm_user_logout(){
	global $context;
	$context->sessions->update('sugm_uid',false);
	$context->sessions->update('sugm_lastlog',time());
	sugm_current_load();
	return true;
}

function sugm_user_activation($username='',$key=''){
	global $context;
	$uid = $context->sessions->get('sugm_uid');
	$sql = "Select * From {$Config['db_table_userkey']} uk "
	." Where user_id = '$uid' and status = 0 ";
	$userRs = $context->storage->query($sql);
	if(PEAR::isError($userRs)){
		return false;
	}
	if($userRs->Count != 1){
		return false;
	}
	$userKeyRow = $userRs->fetchRow();
	if($userKeyRow['key'] != $key){
		return false;
	}
	// Update Key 
	$context->storage->execute("Update {$Config['db_table_userkey']} Set status = 1 Where user_id = '$uid' and key = '".addslashes($key)."' ");
	return true;
}
function sugm_user_generate_key($username=''){
	global $context;
	
	$uid = $context->sessions->get('sugm_uid');
	$sql = "Select * From {$Config['db_table_userkey']} uk "
	." Where user_id = '$uid' and status = 0 ";
	$userRs = $context->storage->query($sql);
	if(PEAR::isError($userRs)){
		return false;
	}
	if($userRs->Count >0){
		return false;
	}
	$time = time();
	$code = md5($time+rand(100000,500000));
	$hash_start = rand(2,8);
	$key = substr($code,7-$hash_start,16-$hash_start) . substr($code,32-$hash_start,40-$hash_start);
	$sql = "Insert Into {$Config['db_table_userkey']}  "
	." (user_id,key,status,log_time,ipaddr) Values "
	." ({$uid}, '{$key}',0,{$time},'{$_SERVER['REMOTE_ADDR']}' ) ";
	$context->storage->execute($sql);
	return true;
}

?>